package euler.p001_050;

import euler.MainEuler;
import euler.helper.NaturalHelper;

public class Euler004 extends MainEuler {
    /*
        A palindromic number reads the same both ways.
        The largest palindrome made from the product
        of two 2-digit numbers is 9009 = 91 × 99.

        Find the largest palindrome made from
        the product of two 3-digit numbers.

     */
    public String resolve(int digitos) {

    	int maxDigitos = (int)Math.pow(10, digitos) - 1;
    	int minDigitos = (int)Math.pow(10, digitos - 1) - 1;

        int max = 0;
        int m, n;
        for (int i = maxDigitos; i > minDigitos; i--) {
            n = max/i;
            for (int j = i; j > ((n >= minDigitos) ? n : minDigitos); j--) {
                m = j * i;
                if (m > max && NaturalHelper.isPalindromo(m)) {
                    max = m;
                }
            }
        }

        return String.valueOf(max);
    }

}
